package com.manage.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manage.platform.context.UserContext;
import com.manage.platform.dto.DeclareDto;
import com.manage.platform.dto.query.DeclarePageQuery;
import com.manage.platform.entity.DeclareEntity;
import com.manage.platform.mapper.DeclareMapper;
import com.manage.platform.utils.RPage;
import com.manage.platform.vo.DeclareVo;
import com.manage.platform.vo.UserVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * YWD
 * 2025/9/28
 */
@AllArgsConstructor
@Service
public class DeclareServiceImpl extends ServiceImpl<DeclareMapper, DeclareEntity> {

    public RPage<List<DeclareVo>> getDeclarePage(DeclarePageQuery query) {
        LambdaQueryWrapper<DeclareEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(DeclareEntity::getIsDeleted, 0)
                .eq(ObjectUtil.isNotEmpty(query.getType()),DeclareEntity::getType, query.getType())
                .like(ObjectUtil.isNotEmpty(query.getName()),DeclareEntity::getName, query.getName());
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<DeclareEntity> list = list(queryWrapper);
        List<DeclareVo> list1 = BeanUtil.copyToList(list, DeclareVo.class);
        return RPage.ok(list1, query.getPage(), query.getLimit(), page.getTotal());
    }

    public DeclareVo getDeclareDetails(Long id) {
        return BeanUtil.copyProperties(this.getById(id), DeclareVo.class);
    }

    public Boolean saveDeclare(DeclareDto dto) {
        UserVo userVo = UserContext.getUser();
        dto.setUserId(userVo.getId());
        return save(BeanUtil.copyProperties(dto, DeclareEntity.class));
    }

    public Boolean updateDeclare(DeclareDto dto) {
        return updateById(BeanUtil.copyProperties(dto, DeclareEntity.class));
    }

    public Boolean deleteDeclare(Long id) {
        return removeById(id);
    }

    public Boolean deleteDeclareBatch(List<Long> ids) {
        return removeByIds(ids);
    }
}
